$(document).ready(function () {
    query();

});


/*查询全部公告*/
var query = function () {
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.noticeList",
        "sAjaxSource": "http://localhost:8080/notice/queryNotice",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>"
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<a href='#'><span  class='label label-danger mr10 mb10' id='delete'>删除</span></a>" +
                "<a href='#'><span  class='label label-yellow mr10 mb10' id='update'>修改</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "noticeId"},
            {"mDataProp": "teName"},
            {"mDataProp": "noticeAuthor"},
            {"mDataProp": "noticeDatetime"},
            {"mDataProp": "noticeTitle"},
            {
                "mDataProp": "noticeContent",
                "mRender": function (data) {
                    return html_decode(data);
                }
            },
            {"mDataProp": ""},
        ],
    })
};


function html_decode(str) {
    var s = "";
    if (str.length == 0)
        return "";
    s = str.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
}
/*
$(document).on('click', '#delete', function () {
    //获取$noticeId
    var $noticeId = $(this).parents("tr").children(0).eq(0).text();
    $.ajax({
        url: "http://localhost:8080/notice/deleteNotice",
        type: "GET",
        async: true,
        data: "noticeId=" + $noticeId,
        dataType: "json",
        success: function () {
            location.reload();
            layer.alert("操作成功!");
            layer.confirm('确定删除？',{
                btn:['确认','取消']
            },function () {

            })
        },
        error: function () {
            layer.alert("操作失败!");
        }
    });
});*/

$(document).on('click', '#delete', function () {
    var $noticeId = $(this).parents("tr").children(0).eq(0).text();
    layer.confirm('确定删除？',{
        btn:['确认','取消']
    },function () {
        $.ajax({
            url: "http://localhost:8080/notice/deleteNotice",
            type: "GET",
            async: true,
            data: "noticeId=" + $noticeId,
            dataType: "json",
            success: function () {
                location.reload();
                layer.msg("操作成功!",{icon:1});
            },
            error: function () {
                layer.alert("操作失败!");
            }
        });
    })
});


$(document).on('click', '#update', function () {
    //获取$noticeId
    var $noticeId = $(this).parents("tr").children(0).eq(0).text();
    //页面层
    layer.open({
        type: 2,
        title: '更新公告',
        area: ['800px', '400px'],
        content: ['noticeEdit.html?noticeId='+$noticeId, 'no']
    })
})